package com.sanpu.kxe.business.main.view;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.sanpu.kxe.R;
import com.sanpu.kxe.base.BaseTitleActivity;
import com.sanpu.kxe.business.main.adapter.MyScoreAdapter;
import com.sanpu.kxe.business.main.bean.PointDetailOutput;
import com.sanpu.kxe.business.main.bean.PointTotalOutput;
import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.contract.MyScoreContract;
import com.sanpu.kxe.business.main.presenter.MyScorePresenter;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.module.immersionbar.ImmersionBar;
import com.sanpu.kxe.module.scrollable.CanScrollVerticallyDelegate;
import com.sanpu.kxe.module.scrollable.OnFlingOverListener;
import com.sanpu.kxe.module.scrollable.OnScrollChangedListener;
import com.sanpu.kxe.module.scrollable.ScrollableLayout;
import com.sanpu.kxe.module.widget.NumberRunningTextView;
import com.sanpu.kxe.module.widget.VerticalSwipeRefreshLayout;
import com.sanpu.kxe.module.widget.WaveHelper;
import com.sanpu.kxe.module.widget.WaveView;

import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Description：我的积分
 * Created by 薛勇军 on 2018/7/18.
 */

public class MyScoreActivity extends BaseTitleActivity implements MyScoreContract.View,
        CanScrollVerticallyDelegate, OnFlingOverListener {

    @Bind(R.id.list_view)
    RecyclerView vListView;
    @Bind(R.id.swipeRefreshLayout)
    VerticalSwipeRefreshLayout vSwipeRefreshLayout;
    @Bind(R.id.user_score)
    NumberRunningTextView vUserScore;
    @Bind(R.id.count_score)
    TextView vCountScore;
    @Bind(R.id.month_score)
    TextView vMonthScore;
    @Bind(R.id.scrollable_layout)
    ScrollableLayout vScrollableLayout;
    @Bind(R.id.navigation)
    LinearLayout vNavigation;
    @Bind(R.id.header)
    FrameLayout vHeader;
    @Bind(R.id.wave)
    WaveView vWave;

    private MyScoreAdapter myScoreAdapter;

    private MyScoreContract.Presenter mPresenter;

    private int nowPage = 0;

    @Override
    protected int getLayoutResID() {
        return R.layout.activity_my_score;
    }

    @Override
    protected void initView() {
        super.initView();
        vListView.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        myScoreAdapter = new MyScoreAdapter();
        vListView.setAdapter(myScoreAdapter);
        myScoreAdapter.disableLoadMoreIfNotFullPage(vListView);

        vScrollableLayout.setDraggableView(vNavigation);

        WaveHelper mWaveHelper = new WaveHelper(vWave);
        vWave.setShapeType(WaveView.ShapeType.SQUARE);
        mWaveHelper.start();

        mPresenter = new MyScorePresenter(this);
        mPresenter.pointTotal();
        mPresenter.pointDetailList(nowPage);
    }

    @Override
    protected void initData() {
        super.initData();
        UserCenterOutput output = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (output != null) {
            vUserScore.setContent(output.getCurBp() + "");
        }
    }

    @Override
    protected void setListener() {
        super.setListener();
        ButterKnife.findById(this, R.id.leftIv).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        vScrollableLayout.setCanScrollVerticallyDelegate(this);
        vScrollableLayout.setOnFlingOverListener(this);
        vScrollableLayout.setOnScrollChangedListener(new OnScrollChangedListener() {
            @Override
            public void onScrollChanged(int y, int oldY, int maxY) {
                final float tabsTranslationY;
                if (y < maxY) {
                    tabsTranslationY = .0F;
                } else {
                    tabsTranslationY = y - maxY;
                }

                vNavigation.setTranslationY(tabsTranslationY);
                vHeader.setTranslationY(y / 2);
            }
        });
        vSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                nowPage = 0;
                mPresenter.pointDetailList(nowPage);
                myScoreAdapter.setEnableLoadMore(true);
            }
        });
        myScoreAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.pointDetailList(nowPage);
            }
        }, vListView);
        myScoreAdapter.setPreLoadNumber(2);
    }

    @Override
    public void showPointView(PointTotalOutput totalOutput) {
        if (totalOutput != null) {
            if (!TextUtils.isEmpty(totalOutput.getSumBp())) {
                vCountScore.setText(totalOutput.getSumBp());
            }
            if (!TextUtils.isEmpty(totalOutput.getCurMonthSumBp())) {
                vMonthScore.setText(totalOutput.getCurMonthSumBp());
            }
        }
    }

    @Override
    public void showPointListView(List<PointDetailOutput> detailOutputs, boolean isFirst) {
        if (detailOutputs != null && detailOutputs.size()>0) {
            if (isFirst) {
                myScoreAdapter.setNewData(detailOutputs);
            } else {
                myScoreAdapter.addData(detailOutputs);
            }
            nowPage++;
        }
    }

    @Override
    public void showLoadMore(String loadMore) {
        if (vSwipeRefreshLayout.isRefreshing()) {
            vSwipeRefreshLayout.setRefreshing(false);
        }
        switch (loadMore) {
            case CommonConfig.LOADMORECOMPLETE:
                myScoreAdapter.loadMoreComplete();
                break;
            case CommonConfig.LOADMOREEND:
                myScoreAdapter.loadMoreEnd();
                break;
            case CommonConfig.LOADMOREFAIL:
                myScoreAdapter.loadMoreFail();
                break;
        }
    }

    @Override
    public boolean canScrollVertically(int direction) {
        return vListView != null && vListView.canScrollVertically(direction);
    }

    @Override
    public void onFlingOver(int y, long duration) {
        if (vListView != null) {
            vListView.smoothScrollBy(y, (int) duration);
        }
    }

    @Override
    protected void initImmersionBar() {
        mImmersionBar = ImmersionBar.with(this);
        mImmersionBar.statusBarColorTransformEnable(true)
                .statusBarView(R.id.topView)
                .statusBarColor(R.color.color_base)
//                .keyboardEnable(true)
                .navigationBarWithKitkatEnable(false)
                .init();
    }

    @Override
    protected boolean hasBindEventBus() {
        return false;
    }

    @Override
    protected boolean hasCommonTitle() {
        return false;
    }

    @Override
    public void setPresenter(MyScoreContract.Presenter presenter) {
        this.mPresenter = presenter;
    }

    @Override
    public Context getContext() {
        return this;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (mPresenter != null) {
            mPresenter.onDestroyPresenter();
            mPresenter = null;
        }
    }
}
